package org.example;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
    public static void main(String[] args) {
package com.yourpackage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

        public class Main {
            public static void main(String[] args) {
                // 获取数据库连接
                try (Connection connection = DBUtil.getConnection()) {
                    if (connection != null) {
                        System.out.println("成功连接到 MySQL 数据库！");

                        // 测试插入用户
                        String insertSQL = "INSERT INTO users (name, email, country) VALUES (?, ?, ?)";
                        try (PreparedStatement preparedStatement = connection.prepareStatement(insertSQL)) {
                            preparedStatement.setString(1, "Alice");
                            preparedStatement.setString(2, "alice@example.com");
                            preparedStatement.setString(3, "USA");
                            int rowsInserted = preparedStatement.executeUpdate();
                            System.out.println("成功插入 " + rowsInserted + " 条记录。");
                        }

                        // 测试查询用户
                        String selectSQL = "SELECT * FROM users";
                        try (PreparedStatement preparedStatement = connection.prepareStatement(selectSQL)) {
                            ResultSet resultSet = preparedStatement.executeQuery();
                            while (resultSet.next()) {
                                int id = resultSet.getInt("id");
                                String name = resultSet.getString("name");
                                String email = resultSet.getString("email");
                                String country = resultSet.getString("country");
                                System.out.println("ID: " + id + ", Name: " + name + ", Email: " + email + ", Country: " + country);
                            }
                        }

                    } else {
                        System.out.println("连接失败！");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        }
    }
}